import os, sys



def label_convert():
    print('Starting to convert ...')
    names = os.listdir('./gt')
    for i, name in enumerate(names):
        temp = name.split('_', 1)
        temp = 'res_' + temp[-1]
        with open(os.path.join('./gt', name), 'r') as f:
            lines = f.readlines()
            if not os.path.exists('./result'):
                os.mkdir('./result')
            with open(os.path.join('./result', temp), 'w') as w:
                for line in lines:
                    line = line.rsplit(',')
                    line = line[:8]
                    line = [str(int(l)+1) for l in line]
                    line = ','.join(line)       
                    w.writelines(line+'\n')
    print('Finish converting ...')

def save_as_zipfile(txtPath='./result/', zipName='submit.zip'):
    import zipfile
    with zipfile.ZipFile(zipName, 'w') as tar:
        for txt in os.listdir(txtPath):
            tar.write(os.path.join(txtPath,txt), arcname=txt) # arcname 指定为文件名可以去除zip里的路径
    print('Saving zip file done ...')

def eval_func():
    save_as_zipfile()
    from script import eval
    print('Starting to evaluate ...')
    eval(gtZip='gt.zip', submitZip='submit.zip')



if __name__ == '__main__':
    label_convert()
    eval_func()


